// pages/register/index.js
var _this, js;
const $api = require('../../../../../request/api').API;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    code: '',
    password: '',
    second: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},
  clear() {
    clearInterval(js);
    js = null;
    this.setData({
      second: 0
    });
  },
  getcode() {
    var _this = this;
    if (_this.data.phone.length != 11) {
      wx.showToast({
        icon: 'none',
        title: '手机号不正确'
      });
      return;
    }
    if (_this.data.second > 0) {
      return;
    }
    let params = {
      mobile: this.data.phone,
      smsType: 102
    };
    $api.getPhoneCode(params).then(res => {
      console.log('获取验证码的回调', res);
      if (res.data.status == 0) {
        wx.showToast({
          title: '发送验证码成功',
          status: 'success'
        });

        _this.setData({
          second: 60
        });
        js = setInterval(function () {
          _this.setData({
            second: _this.data.second - 1
          });
          if (_this.data.second == 0) {
            _this.clear();
          }
        }, 1000);
      }
    });
  },
  // 忘记密码
  bindLogin() {
    if (this.data.phone.length != 11) {
      wx.showToast({
        icon: 'none',
        title: '手机号不正确'
      });
      return;
    }
    if (this.data.password.length < 6) {
      wx.showToast({
        icon: 'none',
        title: '密码不能小于6位'
      });
      return;
    }
    if (this.data.code.length == 0) {
      wx.showToast({
        icon: 'none',
        title: '请输入验证码'
      });
      return;
    }
    let params = {
      mobile: this.data.phone,
      newPassword: this.data.password,
      code: this.data.code
    };

    $api.forgetPassword(params).then(res => {
      console.log('忘记密码的回调', res);
      if (res.data.status == 0) {
        wx.showToast({
          title: '修改密码成功',
          status: 'success'
        });
        setTimeout(() => {
          wx.reLaunch({
            url: '../login/index'
          });
        }, 1500);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    this.clear();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  // 输入框绑定值
  phoneInput(e) {
    let value = e.detail.value;
    this.setData({
      phone: value
    });
  },
  passwordInput(e) {
    let value = e.detail.value;
    this.setData({
      password: value
    });
  },
  codeInput(e) {
    let value = e.detail.value;
    this.setData({
      code: value
    });
  }
});
